home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / bfd / format.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-08  |  6.4 KB  |  246 lines

  1. /* Generic BFD support for file formats.
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /*
  22. SECTION
  23.     File Formats
  24.  
  25.     A format is a BFD concept of high level file contents. The
  26.     formats supported by BFD are: 
  27.  
  28.     o bfd_object
  29.  
  30.     The BFD may contain data, symbols, relocations and debug info.
  31.  
  32.     o bfd_archive
  33.  
  34.     The BFD contains other BFDs and an optional index.
  35.  
  36.     o bfd_core
  37.  
  38.     The BFD contains the result of an executable core dump.
  39.  
  40.  
  41. */
  42.  
  43. #include "bfd.h"
  44. #include "sysdep.h"
  45. #include "libbfd.h"
  46.  
  47. extern bfd_target *target_vector[];
  48. extern bfd_target *default_vector[];
  49.  
  50.  
  51. /*
  52. FUNCTION
  53.     bfd_check_format
  54.  
  55. SYNOPSIS
  56.     boolean bfd_check_format(bfd *abfd, bfd_format format);
  57.  
  58. DESCRIPTION
  59.     This routine is supplied a BFD and a format. It attempts to
  60.     verify if the file attached to the BFD is indeed compatible
  61.     with the format specified (ie, one of <<bfd_object>>,
  62.     <<bfd_archive>> or <<bfd_core>>).
  63.  
  64.     If the BFD has been set to a specific @var{target} before the
  65.     call, only the named target and format combination will be
  66.     checked. If the target has not been set, or has been set to
  67.     <<default>> then all the known target backends will be
  68.     interrogated to determine a match. 
  69.  
  70.     The function returns <<true>> on success, otherwise <<false>>
  71.     with one of the following error codes:  
  72.  
  73.     o invalid_operation
  74.     if <<format>> is not one of <<bfd_object>>, <<bfd_archive>>or
  75.     <<bfd_core>>.
  76.  
  77.     o system_call_error
  78.     if an error occured during a read -  even some file mismatches
  79.     can cause system_call_errros
  80.  
  81.     o file_not_recognised
  82.     none of the backends recognised the file format
  83.  
  84.     o file_ambiguously_recognized 
  85.     more than one backend recognised the file format.
  86.  
  87. */
  88.  
  89. boolean
  90. DEFUN(bfd_check_format,(abfd, format),
  91.       bfd *abfd AND
  92.       bfd_format format)
  93. {
  94.   bfd_target **target, *save_targ, *right_targ;
  95.   int match_count;
  96.  
  97.   if (!bfd_read_p (abfd) ||
  98.       ((int)(abfd->format) < (int)bfd_unknown) ||
  99.       ((int)(abfd->format) >= (int)bfd_type_end)) {
  100.     bfd_error = invalid_operation;
  101.     return false;
  102.   }
  103.  
  104.   if (abfd->format != bfd_unknown)
  105.     return (abfd->format == format)? true: false;
  106.  
  107.   /* presume the answer is yes */
  108.   abfd->format = format;
  109.  
  110.   /* If the target type was explicitly specified, just check that target.  */
  111.  
  112.   if (!abfd->target_defaulted) {
  113.     bfd_seek (abfd, (file_ptr)0, SEEK_SET);    /* rewind! */
  114.  
  115.     right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
  116.     if (right_targ) {
  117.       abfd->xvec = right_targ;        /* Set the target as returned */
  118.       return true;            /* File position has moved, BTW */
  119.     }
  120.     abfd->format = bfd_unknown;
  121.     return false;            /* Specified target is not right */
  122.   }
  123.  
  124.   /* Since the target type was defaulted, check them 
  125.      all in the hope that one will be uniquely recognized.  */
  126.  
  127.   save_targ = abfd->xvec;
  128.   match_count = 0;
  129.   right_targ = 0;
  130.  
  131.   for (target = target_vector; *target != NULL; target++) {
  132.     bfd_target *temp;
  133.  
  134.     abfd->xvec = *target;    /* Change BFD's target temporarily */
  135.     bfd_seek (abfd, (file_ptr)0, SEEK_SET);
  136.     temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
  137.     if (temp) {                /* This format checks out as ok! */
  138.       right_targ = temp;
  139.       match_count++;
  140.       /* If this is the default target, accept it, even if other targets
  141.      might match.  People who want those other targets have to set 
  142.      the GNUTARGET variable.  */
  143.       if (temp == default_vector[0])
  144.     break;
  145. #ifdef GNU960
  146.       /* Big- and little-endian b.out archives look the same, but it doesn't
  147.        * matter: there is no difference in their headers, and member file byte
  148.        * orders will (I hope) be handled appropriately by bfd.  Ditto for big
  149.        * and little coff archives.  And the 4 coff/b.out object formats are
  150.        * unambiguous.  So accept the first match we find.
  151.        */
  152.       break;
  153. #endif
  154.     }
  155.   }
  156.  
  157.   if (match_count == 1) {
  158.     abfd->xvec = right_targ;        /* Change BFD's target permanently */
  159.     return true;            /* File position has moved, BTW */
  160.   }
  161.  
  162.   abfd->xvec = save_targ;        /* Restore original target type */
  163.   abfd->format = bfd_unknown;        /* Restore original format */
  164.   bfd_error = ((match_count == 0) ? file_not_recognized :
  165.            file_ambiguously_recognized);
  166.   return false;
  167. }
  168.  
  169.  
  170. /*
  171. FUNCTION
  172.     bfd_set_format
  173.  
  174. SYNOPSIS
  175.     boolean bfd_set_format(bfd *, bfd_format);
  176.  
  177. DESCRIPTION
  178.     This function sets the file format of the supplied BFD to the
  179.     format requested. If the target set in the BFD does not
  180.     support the format requested, the format is illegal or the BFD
  181.     is not open for writing than an error occurs.
  182.  
  183. */
  184.  
  185. boolean
  186. DEFUN(bfd_set_format,(abfd, format),
  187.       bfd *abfd AND
  188.       bfd_format format)
  189. {
  190.  
  191.   if (bfd_read_p (abfd) ||
  192.       ((int)abfd->format < (int)bfd_unknown) ||
  193.       ((int)abfd->format >= (int)bfd_type_end)) {
  194.     bfd_error = invalid_operation;
  195.     return false;
  196.   }
  197.  
  198.   if (abfd->format != bfd_unknown)
  199.     return (abfd->format == format) ? true:false;
  200.  
  201.   /* presume the answer is yes */
  202.   abfd->format = format;
  203.  
  204.   if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd))) {
  205.     abfd->format = bfd_unknown;
  206.     return false;
  207.   }
  208.  
  209.   return true;
  210. }
  211.  
  212.  
  213. /*
  214. FUNCTION
  215.     bfd_format_string
  216.  
  217. SYNOPSIS
  218.     CONST char *bfd_format_string(bfd_format);
  219.  
  220. DESCRIPTION
  221.     This function takes one argument, and enumerated type
  222.     (bfd_format) and returns a pointer to a const string
  223.     <<invalid>>, <<object>>, <<archive>>, <<core>> or <<unknown>>
  224.     depending upon the value of the enumeration.
  225. */
  226.  
  227. CONST char *
  228. DEFUN(bfd_format_string,(format),
  229.      bfd_format format)
  230. {
  231.   if (((int)format <(int) bfd_unknown) 
  232.       || ((int)format >=(int) bfd_type_end)) 
  233.     return "invalid";
  234.   
  235.   switch (format) {
  236.   case bfd_object:
  237.     return "object";        /* linker/assember/compiler output */
  238.   case bfd_archive: 
  239.     return "archive";        /* object archive file */
  240.   case bfd_core: 
  241.     return "core";        /* core dump */
  242.   default: 
  243.     return "unknown";
  244.   }
  245. }
  246.